package com.wlwx.simulator.message.inbound;

import com.wlwx.simulator.codec.CMPPCommand;
import com.wlwx.simulator.message.basic.BaseInBoundMessage;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * 字段名	字节数	属性	描述
 * Msg_Id	8	Unsigned Integer	信息标识，由SP侧短信网关本身产生，本处填空。
 * Pk_total	1	Unsigned Integer	相同Msg_Id的信息总条数，从1开始
 * Pk_number	1	Unsigned Integer	相同Msg_Id的信息序号，从1开始
 * Registered_Delivery	1	Unsigned Integer	是否要求返回状态确认报告：
 * Msg_level	1	Unsigned Integer	信息级别
 * Service_Id	10	Octet String	业务类型，是数字、字母和符号的组合。
 * Fee_UserType	1	Unsigned Integer	计费用户类型字段
 * Fee_terminal_Id	21	Unsigned Integer	被计费用户的号码（如本字节填空，则表示本字段无效，对谁计费参见Fee_UserType字段，本字段与Fee_UserType字段互斥）
 * TP_pId	1	Unsigned Integer	GSM协议类型。详细是解释请参考GSM03.40中的9.2.3.9
 * TP_udhi	1	Unsigned Integer	GSM协议类型。详细是解释请参考GSM03.40中的9.2.3.23,仅使用1位，右对齐
 * Msg_Fmt	1	Unsigned Integer	信息格式
 * Msg_src	6	Octet String	信息内容来源(SP_Id)
 * FeeType	2	Octet String	资费类别
 * FeeCode	6	Octet String	资费代码（以分为单位）
 * ValId_Time	17	Octet String	存活有效期，格式遵循SMPP3.3协议
 * At_Time	17	Octet String	定时发送时间，格式遵循SMPP3.3协议
 * Src_Id	21	Octet String	源号码
 * DestUsr_tl	1	Unsigned Integer	接收信息的用户数量(小于100个用户)
 * Dest_terminal_Id	21*DestUsr_tl	Octet String	接收短信的MSISDN号码
 * Msg_Length	1	Unsigned Integer	信息长度(Msg_Fmt值为0时：<160个字节；其它<=140个字节)
 * Msg_Content	Msg_length	Octet String	信息内容
 * Reserve	8	Octet String	保留
 */
public class SubmitInBoundRequest extends BaseInBoundMessage {
    private long    msgId;
    private int     pkTotal;
    private int     pkNumber;
    private int     registeredDelivery;
    private int     msgLevel;
    private String  serviceId;
    private int     feeUserType;
    private String  feeTerminalId;
    private byte    tpPid;
    private byte    tpUhi;
    private byte    msgFmt;
    private String  msgSrc;
    private String  feeType;
    private String  feeCode;
    private String  valIdTime;
    private String  atTime;
    private String  srcId;
    private byte    destUrlTl;
    private String  destTerminalId;
    private byte    msgLength;
    private String  msgContent;
    private String  reserve;

    public SubmitInBoundRequest() {
        super(CMPPCommand.CMPP_SUBMIT_REQ_COMMAND);
    }

    public void decode(ByteBuffer byteBuffer) {
        byte[] bytes = null;
        msgId              = byteBuffer.getLong();
        pkTotal            = byteBuffer.get();
        pkNumber           = byteBuffer.get();
        registeredDelivery = byteBuffer.get();
        msgLevel           = byteBuffer.get();
        serviceId          = getString(byteBuffer, 10);
        feeUserType        = byteBuffer.get();
        feeTerminalId      = getString(byteBuffer, 21);
        tpPid              = byteBuffer.get();
        tpUhi              = byteBuffer.get();
        msgFmt             = byteBuffer.get();
        msgSrc             = getString(byteBuffer, 6);
        feeType            = getString(byteBuffer, 2);
        feeCode            = getString(byteBuffer, 6);
        valIdTime          = getString(byteBuffer, 17);
        atTime             = getString(byteBuffer, 17);
        srcId              = getString(byteBuffer, 21);
        destUrlTl          = byteBuffer.get();
        destTerminalId     = getString(byteBuffer, 21);
        msgLength          = byteBuffer.get();
        msgContent         = getString(byteBuffer, msgLength, StandardCharsets.UTF_16BE);
        reserve            = getString(byteBuffer, 8);
    }

    private String getString(ByteBuffer byteBuffer, int len) {
        return getString(byteBuffer, len, StandardCharsets.UTF_8);
    }

    private String getString(ByteBuffer byteBuffer, int len, Charset charset) {
        byte[] bytes = new byte[len];
        byteBuffer.get(bytes);
        return new String(bytes, charset);
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer("CMPP20 SUBMIT REQ : [");
        sb.append("msgId")             .append(":").append(msgId             ).append(",");
        sb.append("pkTotal")           .append(":").append(pkTotal           ).append(",");
        sb.append("pkNumber")          .append(":").append(pkNumber          ).append(",");
        sb.append("registeredDelivery").append(":").append(registeredDelivery).append(",");
        sb.append("msgLevel")          .append(":").append(msgLevel          ).append(",");
        sb.append("serviceId")         .append(":").append(serviceId         ).append(",");
        sb.append("feeUserType")       .append(":").append(feeUserType       ).append(",");
        sb.append("feeTerminalId")     .append(":").append(feeTerminalId     ).append(",");
        sb.append("tpPid")             .append(":").append(tpPid             ).append(",");
        sb.append("tpUhi")             .append(":").append(tpUhi             ).append(",");
        sb.append("msgFmt")            .append(":").append(msgFmt            ).append(",");
        sb.append("msgSrc")            .append(":").append(msgSrc            ).append(",");
        sb.append("feeType")           .append(":").append(feeType           ).append(",");
        sb.append("feeCode")           .append(":").append(feeCode           ).append(",");
        sb.append("valIdTime")         .append(":").append(valIdTime         ).append(",");
        sb.append("atTime")            .append(":").append(atTime            ).append(",");
        sb.append("srcId")             .append(":").append(srcId             ).append(",");
        sb.append("destUrlTl")         .append(":").append(destUrlTl         ).append(",");
        sb.append("destTerminalId")    .append(":").append(destTerminalId    ).append(",");
        sb.append("msgLength")         .append(":").append(msgLength         ).append(",");
        sb.append("msgContent")        .append(":").append(msgContent        ).append(",");
        sb.append("reserve")           .append(":").append(reserve           );
        sb.append("]");
        return sb.toString();
    }

    public long getMsgId() {
        return msgId;
    }

    public void setMsgId(long msgId) {
        this.msgId = msgId;
    }

    public int getPkTotal() {
        return pkTotal;
    }

    public void setPkTotal(int pkTotal) {
        this.pkTotal = pkTotal;
    }

    public int getPkNumber() {
        return pkNumber;
    }

    public void setPkNumber(int pkNumber) {
        this.pkNumber = pkNumber;
    }

    public int getRegisteredDelivery() {
        return registeredDelivery;
    }

    public void setRegisteredDelivery(int registeredDelivery) {
        this.registeredDelivery = registeredDelivery;
    }

    public int getMsgLevel() {
        return msgLevel;
    }

    public void setMsgLevel(int msgLevel) {
        this.msgLevel = msgLevel;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public int getFeeUserType() {
        return feeUserType;
    }

    public void setFeeUserType(int feeUserType) {
        this.feeUserType = feeUserType;
    }

    public String getFeeTerminalId() {
        return feeTerminalId;
    }

    public void setFeeTerminalId(String feeTerminalId) {
        this.feeTerminalId = feeTerminalId;
    }

    public byte getTpPid() {
        return tpPid;
    }

    public void setTpPid(byte tpPid) {
        this.tpPid = tpPid;
    }

    public byte getTpUhi() {
        return tpUhi;
    }

    public void setTpUhi(byte tpUhi) {
        this.tpUhi = tpUhi;
    }

    public byte getMsgFmt() {
        return msgFmt;
    }

    public void setMsgFmt(byte msgFmt) {
        this.msgFmt = msgFmt;
    }

    public String getMsgSrc() {
        return msgSrc;
    }

    public void setMsgSrc(String msgSrc) {
        this.msgSrc = msgSrc;
    }

    public String getFeeType() {
        return feeType;
    }

    public void setFeeType(String feeType) {
        this.feeType = feeType;
    }

    public String getFeeCode() {
        return feeCode;
    }

    public void setFeeCode(String feeCode) {
        this.feeCode = feeCode;
    }

    public String getValIdTime() {
        return valIdTime;
    }

    public void setValIdTime(String valIdTime) {
        this.valIdTime = valIdTime;
    }

    public String getAtTime() {
        return atTime;
    }

    public void setAtTime(String atTime) {
        this.atTime = atTime;
    }

    public String getSrcId() {
        return srcId;
    }

    public void setSrcId(String srcId) {
        this.srcId = srcId;
    }

    public byte getDestUrlTl() {
        return destUrlTl;
    }

    public void setDestUrlTl(byte destUrlTl) {
        this.destUrlTl = destUrlTl;
    }

    public String getDestTerminalId() {
        return destTerminalId;
    }

    public void setDestTerminalId(String destTerminalId) {
        this.destTerminalId = destTerminalId;
    }

    public byte getMsgLength() {
        return msgLength;
    }

    public void setMsgLength(byte msgLength) {
        this.msgLength = msgLength;
    }

    public String getMsgContent() {
        return msgContent;
    }

    public void setMsgContent(String msgContent) {
        this.msgContent = msgContent;
    }

    public String getReserve() {
        return reserve;
    }

    public void setReserve(String reserve) {
        this.reserve = reserve;
    }
}
